/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.matrixxun.starry.provider;

import com.matrixxun.starry.FixedComponentInfo;

import ohos.agp.components.*;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * 顶部布局适配器
 */
public class ListHeaderProvider extends BaseItemProvider {
    private static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP, 0x20221, "ListHeaderProvider");
    static final ArrayList<FixedComponentInfo> EMPTY_INFO_LIST = new ArrayList<FixedComponentInfo>();
    Context mContext;
    List<FixedComponentInfo> mFixedComponents;
    BaseItemProvider mProvider;
    private static final int TYPE_HEADER = 1001;
    private static final int TYPE_ITEM = 1002;

    public ListHeaderProvider(
            Context context, ArrayList<FixedComponentInfo> fixedComponentInfos, BaseItemProvider listProvider) {
        mContext = context;
        if (fixedComponentInfos == null) {
            mFixedComponents = EMPTY_INFO_LIST;
        } else {
            mFixedComponents = fixedComponentInfos;
        }
        mProvider = listProvider;
    }

    @Override
    public int getCount() {
        if (mProvider == null) {
            return mFixedComponents.size();
        } else {
            return mFixedComponents.size() + mProvider.getCount();
        }
    }

    @Override
    public Object getItem(int position) {
        if (position < mFixedComponents.size()) {
            return mFixedComponents.get(position).data;
        } else {
            if (mProvider != null) {
                int listIndex = position - mFixedComponents.size();
                if (listIndex < mProvider.getCount()) {
                    return mProvider.getItem(listIndex);
                }
            }
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        if (mProvider != null && position >= mFixedComponents.size()) {
            int adjPosition = position - mFixedComponents.size();
            int adapterCount = mProvider.getCount();
            if (adjPosition < adapterCount) {
                return mProvider.getItemId(adjPosition);
            }
        }
        return -1;
    }

    @Override
    public Component getComponent(int index, Component component, ComponentContainer componentContainer) {
        HiLog.info(LOG_LABEL, "getComponent : " + index);
        switch (getItemComponentType(index)){
            case TYPE_HEADER:
                if (index < mFixedComponents.size()) {
                    return mFixedComponents.get(index).component;
                }
                break;
            case TYPE_ITEM:
                if (mProvider != null && index - mFixedComponents.size() < mProvider.getCount()) {
                    return mProvider.getComponent(index - mFixedComponents.size(), component, componentContainer);
                }
                break;
        }
        return component;
    }

    /**
     * 去除顶部图片布局
     * @param component 被删除的图片资源
     * @return true：删除成功 false：删除失败
     */
    public boolean removeHeader(Component component) {
        for (int i = 0; i < mFixedComponents.size(); i++) {
            FixedComponentInfo info = mFixedComponents.get(i);
            if (info.component == component) {
                mFixedComponents.remove(i);
                return true;
            }
        }
        return false;
    }

    @Override
    public int getItemComponentType(int position) {
        if (position < mFixedComponents.size()){
            return TYPE_HEADER;
        }else{
            return TYPE_ITEM;
        }
    }
}
